﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WorldView
{
    public partial class SignWindow : Form
    {
       
        Varies v;
        public SignWindow(Varies v1)
        {
            v = v1;
            InitializeComponent();
            for (int i = 0; i < 1000; i++)
            {

                SignList.Items.Add("Sign "+i);

            }
        }

        

        private void SignBox_TextChanged(object sender, EventArgs e)
        {
            if (SignBox.Text.Length > 0)
            {
                v.signs[SignList.SelectedIndex].Text = SignBox.Text;
            }
        }

        private void XLoc_TextChanged(object sender, EventArgs e)
        {
            if (XLoc.Text.Length > 0)
            {
                v.signs[SignList.SelectedIndex].X = Convert.ToInt32(XLoc.Text);
                if (Convert.ToInt32(XLoc.Text) != 0 && Convert.ToInt32(YLoc.Text) != 0)
                {
                    v.signs[SignList.SelectedIndex].isActive = true;
                    SignBox.Enabled = true;
                }
            }
        }

        private void YLoc_TextChanged(object sender, EventArgs e)
        {
            if (YLoc.Text.Length > 0)
            {
                v.signs[SignList.SelectedIndex].Y = Convert.ToInt32(YLoc.Text);
                if (Convert.ToInt32(XLoc.Text) != 0 && Convert.ToInt32(YLoc.Text) != 0)
                {
                    v.signs[SignList.SelectedIndex].isActive = true;
                    SignBox.Enabled = true;
                }
            }
        }

        private void SignList_SelectedIndexChanged(object sender, EventArgs e)
        {
            XLoc.Text = v.signs[SignList.SelectedIndex].X.ToString();
            YLoc.Text = v.signs[SignList.SelectedIndex].Y.ToString();
            SignBox.Text = v.signs[SignList.SelectedIndex].Text;
            XLoc.Enabled = true;
            YLoc.Enabled = true;
            if (Convert.ToInt32(XLoc.Text) != 0 && Convert.ToInt32(YLoc.Text) != 0)
            {
                SignBox.Enabled = true;
            }
            else
            {
                SignBox.Enabled = false;
            }
        }

        private void ExportButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialogW = new SaveFileDialog();
            dialogW.Filter = "Terraria Sign File (*.sign)|*.sign";
            dialogW.Title = "Select Sign File";
            dialogW.FileName = string.Format("{0}.sign", System.IO.Path.GetFileNameWithoutExtension(""));
            string filePath = (dialogW.ShowDialog() == DialogResult.OK) ? dialogW.FileName : string.Empty;
            if (filePath == string.Empty)
            {
                return;
            }
            else if (new System.IO.FileInfo(filePath).Extension.ToUpper() != ".SIGN")
            {
                MessageBox.Show("Output File MUST End in .SIGN", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (FileStream stream = new FileStream(filePath, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        writer.Write(v.signs[i].isActive);
                        if (v.signs[i].isActive)
                        {
                            writer.Write(v.signs[i].Text);
                            writer.Write(v.signs[i].X);
                            writer.Write(v.signs[i].Y);
                        }
                    }
                    writer.Close();
                }
                stream.Close();

            }
        }

        private void ImportButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogW = new OpenFileDialog();
            dialogW.Filter = "Terraria Sign File (*.sign)|*.sign";
            dialogW.Title = "Select Sign File";
            dialogW.FileName = string.Format("{0}.sign", System.IO.Path.GetFileNameWithoutExtension(""));
            string filePath = (dialogW.ShowDialog() == DialogResult.OK) ? dialogW.FileName : string.Empty;
            if (filePath == string.Empty)
            {
                return;
            }
            else if (new System.IO.FileInfo(filePath).Extension.ToUpper() != ".SIGN")
            {
                MessageBox.Show("Output File MUST End in .SIGN", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        bool active = reader.ReadBoolean();
                        v.signs[i].isActive = active;
                        string text = "";
                        int X = 0;
                        int Y = 0;
                        if (active)
                        {
                            text = reader.ReadString();
                            X = reader.ReadInt32();
                            Y = reader.ReadInt32();

                            v.signs[i].Text = text;
                            v.signs[i].X = X;
                            v.signs[i].Y = Y;
                        }
                    }
                    reader.Close();
                }
                stream.Close();

            }
        }
    }
}
